package com.app.micaihu.view.army.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.configure.LoadType
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.army.contract.ArmyMessageListContract
import com.app.micaihu.view.bean.ArmyMessage
import com.app.utils.biz.Biz

/**
 * 军团消息列表
 */
open class ArmyMessageListPresenter : BasePresenter<ArmyMessageListContract.View>(), ArmyMessageListContract.Presenter {

    private var isRefresh = true
    private var page = 1
    private var firstLoad = true

    override fun armyMessageList() {
        if (firstLoad) {
            getView()?.showLoading()
            firstLoad = false
        }
        val params = ParamsMapUtils.commonParams
        params["page"] = page
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyMessageList(params), object : NetRequestListener<BaseBean<List<ArmyMessage>?>?> {
            override fun onSuccess(result: BaseBean<List<ArmyMessage>?>?) {
                getView()?.hideLoading()
                page++
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_SUCCESS else LoadType.TYPE_LOAD_MORE_SUCCESS
                getView()?.handleArmyMessageList(result, loadType)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_ERROR else LoadType.TYPE_LOAD_MORE_ERROR
                getView()?.handleArmyMessageList(null, loadType)
                getView()?.onError(e)
            }
        })
    }

    override fun refresh() {
        page = 1
        isRefresh = true
        armyMessageList()
    }

    override fun loadMore() {
        isRefresh = false
        armyMessageList()
    }

    /**
     * //type 1同意 2拒绝
     */
    override fun armyAuditing(gid: Int, toUid: String, type: Int) {
        getView()?.showLoading()
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["to_uid"] = toUid
        params["type"] = type
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyAuditing(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleArmyAuditing(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmyAuditing(null)
                getView()?.onError(e)
            }
        })
    }


}